﻿
function UploadImage(imageUploadField, valueField, uploaderUrl, buttonImage, isSignalImage, imgDiv) {
    $("#" + imageUploadField).uploadify({
        queueID: '1',
        swf: '/Scripts/uploadify/uploadify.swf',
        uploader: uploaderUrl,
        buttonImage: buttonImage, // button image
        fileSizeLimit: '100000000KB', // max size
        fileTypeDesc: 'Image Files',
        fileTypeExts: '*.gif; *.jpg; *.png; *.jpeg',
        auto: true, // auto upload
        uploadLimit: 20,
        queueSizeLimit: 5,
        width: 75, // button width
        height: 21, // button height
        onSelect: function (file) {
            var name = file.name;
            if (name.length >= 10) {
                var index = name.lastIndexOf(".");
                name = name.substr(0, 8) + "...." + name.substr(index + 1);
            }
            var html;
            if (isSignalImage) {
                html = '<div id="div_id_' + file.id + '" class="uploading"><div id="div_title_' + file.id + '" class="uploading_file_name">' + name + '<div><div id="div_uploading_' + file.id + '" class="uploading_schedule"></div></div>';
                $("#" + imgDiv).html(html);
            } else {
                html = '<div id="div_id_' + file.id + '" class="uploading"><div id="div_title_' + file.id + '" class="uploading_file_name">' + name + '<div><div id="div_uploading_' + file.id + '" class="uploading_schedule"></div></div>';
                $("#" + imgDiv).append(html);
            }
        },
        onUploadProgress: function (file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) {
            //var v = Math.round(bytesUploaded / bytesTotal * 100);
            //$('#div_uploading_' + file.id).html(v + "%");
        },
        onUploadSuccess: function (file, data, response) {
            var html = '<img alt="" src="' + data + '" />';
            html += "<a href=\"javascript:\" onclick=\"DeleteFile('" + file.id + "');\">X</a>";
            $("#div_id_" + file.id).html(html);
            $("#div_title_" + file.id).remove();
            $("#div_uploading_" + file.id).remove();
            if (isSignalImage) {
                $("#" + valueField).val(data);
            } else {
                var galleryArray;
                if ($("#" + valueField).val() === '') {
                    galleryArray = new Array();
                } else {
                    galleryArray = $("#" + valueField).val().split(',');
                }
                galleryArray.push(data);
                $("#" + valueField).val(galleryArray);
            }
        }
    });
    //alert($("#div_gallery_list > div").size());
    //$('#div_gallery_list').css("display", "block");
}

function DeleteFile(id) {
    $("#div_id_" + id).remove();
}